
import com.wcarmon.life.model.LifeModelLocator;
import com.wcarmon.life.event.ShowOrganizeZoomEvent;
import com.wcarmon.life.event.ShowDoEvent;
import com.wcarmon.life.event.SearchEvent;
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.utils.ObjectUtil;
import mx.utils.StringUtil;
import mx.collections.ArrayCollection;         	
import mx.collections.SortField;
import mx.collections.Sort;
import flash.utils.Timer;
      	  
      	  
include "../../../includes/loggerSnippet.as";      	  
      	         	
      	         	
[Bindable]
private var model : LifeModelLocator = LifeModelLocator.getInstance();

public var MAX_ROW_COUNT_FOR_GRID : uint = 12;
			
private var lastClickTime : int = -1;
private var lastClickedId : int = -1;
			
//////////////////////////////////////////////////////////////////////			
private function doInit() : void {
	contentTN.selectedIndex = 1;
	log.debug('initializing dashboard, setting contentTN.selectedIndex to ' + contentTN.selectedIndex);
}



//////////////////////////////////////////////////////////////////////
private function clearFilterField():void{
	log.debug('clearing filter field');
	if( searchField.text == LifeModelLocator.INITIAL_SEARCH_BOX_STR ){
		searchField.text = '';
	}
}







private function changeSearchMode( m : String ) : void {
	log.debug('changing search mode to ' + m);
	LifeModelLocator.getInstance().ANDMode = ( m == 'and' );
	
	if( ! model.searchIsInProgress ){
		runSearch();
	}else{
		log.warn("You cannot change mode while search in progress");
	}
}

//////////////////////////////////////////////////////////////////////
private function getSearchResults(a : Array) : ArrayCollection {
	log.debug('pulling search results from model (found ' + a.length + ")");
	var ans : ArrayCollection = new ArrayCollection( a );				

	if( a.length > 0 ){
		var sorter : Sort = new Sort();
		var sf1 : SortField = new SortField("state_id", false, false);
		var sf2 : SortField = new SortField("status", false, false);
	
		sorter.fields = [sf1,sf2];
		ans.sort = sorter;
		ans.refresh();
		searchResultsGrid.rowCount = Math.min( MAX_ROW_COUNT_FOR_GRID, a.length+1 );
	}else{
		searchResultsGrid.rowCount = 2;
	}
	log.debug( 'setting search results grid rowCount to ' + searchResultsGrid.rowCount );	
	return ans;
}



//////////////////////////////////////////////////////////////////////
private function runSearch() : void {
	log.debug('running search ...');
	var s : String = StringUtil.trim( searchField.text );
	if( s == LifeModelLocator.INITIAL_SEARCH_BOX_STR ){
		log.debug("Search field still has initial values, ignoring search request");
		//contentTN.selectedIndex = 1;
		return;
	}
	if( model.searchIsInProgress ){
		contentTN.selectedIndex = 0;
		log.info("Search in progress, ignoring search request");
		return;
	}
	contentTN.selectedIndex = 0;
	(new SearchEvent(searchField.text)).dispatch();
} 



private function resetSearch() : void {
	log.debug('resetting search');
	//searchField.text = LifeModelLocator.INITIAL_SEARCH_BOX_STR;
	searchField.text = '';
	contentTN.selectedIndex = 1;
}



//////////////////////////////////////////////////////////////////////
private function canEnableSearchButton(searchIsInProgress : Boolean, s : String) : Boolean {
	var s2 : String = StringUtil.trim(s);
	//return !( searchIsInProgress || s2 =='' || s2==LifeModelLocator.INITIAL_SEARCH_BOX_STR );
	return !( searchIsInProgress || s2==LifeModelLocator.INITIAL_SEARCH_BOX_STR );
}



//////////////////////////////////////////////////////////////////////
private function itemClickEvent(event:ListEvent):void {
   
   log.debug( "Item clicked on grid: col=" + event.columnIndex + ", row=" + event.rowIndex + ", event type=" + event.type + ", selectedItem=" + event.target.selectedItem);

	var hasItem : Boolean = ( event.target.selectedItem != null );
	var isDoubleClick : Boolean = (getTimer() - lastClickTime) < LifeModelLocator.DOUBLE_CLICK_THRESHOLD_IN_MS;
	var isSameItem : Boolean = false; 
	var block_id : uint;
	
	if( 'itemClick' == event.type && hasItem ){
       	block_id = event.target.selectedItem.id;
		
		isSameItem = (lastClickedId == block_id);
		lastClickTime = getTimer();
		lastClickedId = block_id; 
	}
           
	if( isDoubleClick && isSameItem ){
		log.debug('detected doubleclick on block #' + block_id);
		( new ShowOrganizeZoomEvent(block_id) ).dispatch();
	}
}			






//////////////////////////////////////////////////////////////////////
private function formatDates(item:Object, column:DataGridColumn) : String{
	return myDF1.format( item[column.dataField] );
}
private function stateIDtoName(item:Object, column:DataGridColumn) : String{
	return LifeModelLocator.getStateName( item[column.dataField] );
}
private function openClosedRejected(item:Object, column:DataGridColumn) : String{
	return getOpenClosedRejectedStatus( item.isComplete, item.isRejected );
}
private function getOpenClosedRejectedStatus( isComplete : uint, isRejected : uint) : String{
	if( isComplete == 1 ){	return "Complete";
	}else if( isRejected == 1 ){ return "Rejected"; 
	}
	return "Open";
}
private function content_dataTipFunc(item:Object):String {
	return item.content.substr(0, 300);
}
private function sortTheStatus(o1:Object, o2:Object) : int{ 
	var s1 : String = getOpenClosedRejectedStatus( o1.isComplete, o1.isRejected );
	var s2 : String = getOpenClosedRejectedStatus( o2.isComplete, o2.isRejected );
	if( s1 == s2 ){ return 0; }
	 
	return (s1<s2) ? -1 : 1;
}
private function sortTheId(o1:Object, o2:Object) : int{
	return ( int(o1.id) < int(o2.id) ) ? -1 : 1; 
} 





//////////////////////////////////////////////////////////////////////         
private function getSearchSectionLabel( results : Array, q : String, txt:String ) : String {
	var defaultStr : String = 'Search Results';

	var sTrimmed : String = StringUtil.trim( txt );
	var cond1 : Boolean = (sTrimmed == LifeModelLocator.INITIAL_SEARCH_BOX_STR); 
	//var cond2 : Boolean = (sTrimmed == '');	
         	
	var cond3 : Boolean = (results == null);
	var cond4 : Boolean = true;
	if( !cond3 ){
		cond4 = (results.length < 0);
	}

	//if( cond1 || cond2 || cond3 || cond4 ){
	if( cond1 || cond3 || cond4 ){
		return defaultStr;
	}else{
		return results.length + '-results for "' + q + '"';
	}
}	

